package cn.tedu.straw.redis.tag.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-08-10 17:07
 */

@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    /**
     * 向Redis中存入数据
     *
     * @param key   数据的Key
     * @param value 数据的值
     */
    public void set(String key, Serializable value) {
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        ops.set(key, value);
    }

    /**
     * 向Redis中存入List
     *
     * @param key   数据的Key
     * @param value List的元素
     */
    public void rightPush(String key, Serializable value) {
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        ops.rightPush(key, value);
    }
    /**
     * 从Redis中获取数据
     *
     * @param key 数据的Key
     * @return 数据值
     */
    public  Serializable get(String key){
        ValueOperations<String, Serializable> ops=redisTemplate.opsForValue();
        return ops.get(key);
    }

    /**
     * 获取某个list所以数据
     * @param key
     * @return 历史统计和
     */
    public List<Serializable> getList(String key){
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        return ops.range(key,0,ops.size(key));
    }
    /**
     * 删除某个Key对应的数据
     *
     * @param key 数据的Key
     */
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}
